/*
 * 文件名：ArborShowServiceImpl.java	 
 * 时     间：下午8:06:31
 * 作     者：lyf      
 * 版     权： 2012-2022 湖南智软网络科技, 公司保留所有权利.
 * 联     系：http://www.073586.com/
 */
package com.local.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cz073586.tools.util.Tools;
import com.local.dao.mapers.CrossingMapper;
import com.local.dao.mapers.CrossingToEquipmentMapper;
import com.local.dao.mapers.CrossingToStationMapper;
import com.local.dao.mapers.EquipmentToMedieMapper;
import com.local.dao.mapers.LocalEquipmentMapper;
import com.local.dao.mapers.LocalScreenMapper;
import com.local.dao.mapers.LocalStationMapper;
import com.local.entity.vo.ArborDataVo;
import com.local.entity.vo.ArborVo;
import com.local.model.Crossing;
import com.local.model.CrossingToEquipment;
import com.local.model.CrossingToEquipmentExample;
import com.local.model.CrossingToStation;
import com.local.model.CrossingToStationExample;
import com.local.model.EquipmentToMedie;
import com.local.model.EquipmentToMedieExample;
import com.local.model.LocalEquipment;
import com.local.model.LocalStation;
import com.local.model.Screen;
import com.local.model.ScreenExample;
import com.local.page.CrossingPageInfo;
import com.local.page.LocalEquipmentPage;
import com.local.page.LocalScreenPage;
import com.local.page.LocalStationPage;
import com.local.service.face.ArborShowService;
import com.qianhai.common.entity.PageInfo;

/**
 * 类描述:这里是类描述
 * 
 * @ClassName: ArborShowServiceImpl
 * @author lyf
 * @date 2016年5月19日 下午8:06:31
 */
@Service
public class ArborShowServiceImpl implements ArborShowService
{
	
	@Autowired
	private LocalStationMapper stationMapper;
	
	@Autowired
	private CrossingMapper crossinMapper;
	
	@Autowired
	private LocalEquipmentMapper equimentMapper;
	
	@Autowired
	private LocalScreenMapper screntmapper;
	
	@Autowired
	private EquipmentToMedieMapper medieMapper;
	
	@Autowired
	private CrossingToStationMapper stationToCrosMapper;
	
	@Autowired
	private CrossingToEquipmentMapper csToEquimentMapper;
	
	@Override
	public ArborVo queryTopo()
	{
		ArborVo resultVo = new ArborVo();
		
		//构建根节点
		ArborDataVo rootVo = new ArborDataVo();
		rootVo.setColor("red");
		rootVo.setShape("dot");
		rootVo.setLabel("中央管理中心");
		rootVo.setType(0);
		resultVo.getNodes().put("root", rootVo);
		
		//获取所有岗亭列表
		PageInfo<LocalStation> pageInfo = new LocalStationPage<LocalStation>();
		pageInfo.setPageSize(Integer.MAX_VALUE);
		
		//初始化岗亭
		List<LocalStation> stationList = stationMapper.queryByPage(pageInfo);
		for(LocalStation stion : stationList)
		{
			ArborDataVo dataVo = new ArborDataVo();
			dataVo.setColor("green");
			dataVo.setShape("dot");
			dataVo.setLabel(stion.getStationName()+"(岗亭)");
			dataVo.setIp(stion.getEquipmentViewStream());
			dataVo.setType(1);
			dataVo.setUuid(stion.getStationUuid());
			resultVo.getNodes().put(stion.getStationUuid(), dataVo);
			
			//画线
			Map<String, String> rootMap = resultVo.getEdges().get("root");
			if(Tools.isEmpty(rootMap))
			{
				rootMap = new HashMap<String, String>();
				resultVo.getEdges().put("root", rootMap);
			}
			
			rootMap.put(stion.getStationUuid(), "{}");
		}//end for
		
		
		//初始化路口信息
		PageInfo<Crossing> crsPageInfo = new CrossingPageInfo();
		crsPageInfo.setPageSize(Integer.MAX_VALUE);
		List<Crossing> crsLists = crossinMapper.queryByPage(crsPageInfo);
		
		for(Crossing crsInfo : crsLists)
		{
			ArborDataVo dataVo = new ArborDataVo();
			dataVo.setColor("maroon");
			dataVo.setShape("dot");
			dataVo.setIp("");
			dataVo.setLabel(crsInfo.getName()+"(路口)");
			dataVo.setType(2);
			dataVo.setUuid(crsInfo.getUuid());
			resultVo.getNodes().put(crsInfo.getUuid(), dataVo);
		}
		
		//初始化相机
		PageInfo<LocalEquipment> equmentPageInfo = new LocalEquipmentPage<LocalEquipment>();
		equmentPageInfo.setPageSize(Integer.MAX_VALUE);
		List<LocalEquipment> eumentLists =  equimentMapper.queryByPage(equmentPageInfo);
		for(LocalEquipment eqInfo : eumentLists)
		{
			ArborDataVo dataVo = new ArborDataVo();
			dataVo.setColor("violet");
			dataVo.setShape("dot");
			dataVo.setLabel("相机");
			dataVo.setIp(eqInfo.getCameraIp());
			dataVo.setCameraNo(eqInfo.getCameraNo());
			dataVo.setType(3);
			dataVo.setUuid(eqInfo.getEquipmentUuid());
			resultVo.getNodes().put(eqInfo.getEquipmentUuid(), dataVo);
		}
		
		//LED屏幕
		PageInfo<Screen> screnPageInfo = new LocalScreenPage<Screen>();
		screnPageInfo.setPageSize(Integer.MAX_VALUE);
		List<Screen> screntLists = screntmapper.queryByPage(screnPageInfo);
		for(Screen screnInfo : screntLists)
		{
			ArborDataVo dataVo = new ArborDataVo();
			dataVo.setColor("blue");
			dataVo.setShape("dot");
			dataVo.setLabel("LED屏");
			dataVo.setIp(screnInfo.getScreenIp());
			dataVo.setType(5);
			dataVo.setUuid(screnInfo.getUuid());
			resultVo.getNodes().put(screnInfo.getUuid(), dataVo);
		}
		
		//流媒体信息
		PageInfo<EquipmentToMedie> medioPage = new PageInfo<EquipmentToMedie>();
		medioPage.setPageSize(Integer.MAX_VALUE);
		List<EquipmentToMedie> medieLists = medieMapper.queryByPage(medioPage);
		
		for(EquipmentToMedie medioInfo : medieLists)
		{
			ArborDataVo dataVo = new ArborDataVo();
			dataVo.setColor("khaki");
			dataVo.setShape("dot");
			dataVo.setLabel("流媒体");
			dataVo.setType(6);
			dataVo.setUuid(medioInfo.getUuid());
			resultVo.getNodes().put(medioInfo.getUuid(), dataVo);
		}
		
		
		/***********************************************************************/
		//岗亭下的路口
		for(LocalStation stion : stationList)
		{
			CrossingToStationExample ex = new CrossingToStationExample();
			ex.or().andStationUuidEqualTo(stion.getStationUuid());
			List<CrossingToStation> stocLists = stationToCrosMapper.selectByCondition(ex);
			
			for(CrossingToStation mapinfo : stocLists)
			{
				Map<String, String> rootMap = resultVo.getEdges().get(stion.getStationUuid());
				if(Tools.isEmpty(rootMap))
				{
					rootMap = new HashMap<String, String>();
					resultVo.getEdges().put(stion.getStationUuid(), rootMap);
				}
				
				rootMap.put(mapinfo.getCrossingUuid(), "{}");
			}
		}//end station with for
		
		//路口到相机
		for(Crossing crsInfo : crsLists)
		{
			CrossingToEquipmentExample ex = new CrossingToEquipmentExample();
			ex.or().andCrossingUuidEqualTo(crsInfo.getUuid());
			List<CrossingToEquipment> ctoeqLists = csToEquimentMapper.selectByCondition(ex);
			
			for(CrossingToEquipment ctoeqInfo : ctoeqLists)
			{
				Map<String, String> rootMap = resultVo.getEdges().get(crsInfo.getUuid());
				if(Tools.isEmpty(rootMap))
				{
					rootMap = new HashMap<String, String>();
					resultVo.getEdges().put(crsInfo.getUuid(), rootMap);
				}
				
				rootMap.put(ctoeqInfo.getEquipmentUuid(), "{}");
			}
		}//end crossing with for
		
		//相机到流媒体
		for(LocalEquipment eqInfo : eumentLists)
		{
			EquipmentToMedieExample ex = new EquipmentToMedieExample();
			ex.or().andEquimentUuidEqualTo(eqInfo.getEquipmentUuid());
			List<EquipmentToMedie> mdLists = medieMapper.selectByCondition(ex);
			
			for(EquipmentToMedie mdInfo : mdLists)
			{
				Map<String, String> rootMap = resultVo.getEdges().get(eqInfo.getEquipmentUuid());
				if(Tools.isEmpty(rootMap))
				{
					rootMap = new HashMap<String, String>();
					resultVo.getEdges().put(eqInfo.getEquipmentUuid(), rootMap);
				}
				
				rootMap.put(mdInfo.getUuid(), "{}");
			}
		}//end medie with for
		
		//LED屏幕
		for(LocalEquipment eqInfo : eumentLists)
		{
			ScreenExample ex = new ScreenExample();
			ex.or().andCameraNoEqualTo(eqInfo.getCameraNo());
			List<Screen> scrLists = screntmapper.selectByCondition(ex);
			
			for(Screen srInfo : scrLists)
			{
				Map<String, String> rootMap = resultVo.getEdges().get(eqInfo.getEquipmentUuid());
				if(Tools.isEmpty(rootMap))
				{
					rootMap = new HashMap<String, String>();
					resultVo.getEdges().put(eqInfo.getEquipmentUuid(), rootMap);
				}
				
				rootMap.put(srInfo.getUuid(), "{}");
			}
			
		}//end LED with for
		return resultVo;
	}
}
